/// Creator:      gaoruishan
/// CreatDate:    2025-06-20
/// Description:  陪护相关  
Class Nur.MNISV3.Service.Accompany Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 查询陪护
/// w ##class(Nur.MNISV3.Service.Accompany).GetEscortDataV4("","2025-06-20","2025-06-20","8","20665","3","2","{""single"":"""",""multiple"":""1""}")
ClassMethod GetEscortDataV4(episodeId, fromDate, toDate, locId, userId, wardId, hospitalId, extras) As %String
{
    n (episodeId, fromDate, toDate, locId, userId,wardId,hospitalId,extras,%session)
    s $zt="errGetEscortDataV4"
    s ^GRS("GetEscortDataV4")=$lb(episodeId, fromDate, toDate, locId, userId,wardId,hospitalId,extras)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s fromDate=##class(websys.Conversions).DateHtmlToLogical(fromDate)
    s toDate=##class(websys.Conversions).DateHtmlToLogical(toDate)
    //调用纯净版患者列表，
    s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
    d patListObj.Execute(wardId,userId,"Ward",locId)
    s patInfoList=##class(%ListOfDataTypes).%New()
    while(patListObj.Next()){
        s patinfo=##class(Nur.MNISV3.Common.Utils).ResultSet(patListObj)
        s episodeId=patListObj.GetDataByName("episodeId")
        
        s accompanyData=##class(%ListOfDataTypes).%New()
        //1.调用PC查询
        s accomRecordObj=##class(%ResultSet).%New("Nur.NIS.Service.Accom.NCPAccompany:GetNCPAccompanyRecord")
        d accomRecordObj.Execute("","",wardId,"",userId,hospitalId,episodeId)
        while(accomRecordObj.Next()){
            s NCPARRowID=accomRecordObj.GetDataByName("NCPARRowID")
            i NCPARRowID'="" {
                s accomData=##class(Nur.MNISV3.Common.Utils).ResultSet(accomRecordObj)
                d accompanyData.Insert(accomData)
            }
        }
        d patinfo.SetAt($g(accompanyData),"accompanyData")
        //基本患者信息
        s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
        d patInfoList.Insert(patinfo)
    }
    //列配置项
    s accompanyDisplayItems=..GetDisplayElementsByType(hospitalId,"P","NCPARIdNo,NCPAInfo1,NCPAInfo4","NCPAInfo5,NCPAInfo6")
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    d json.SetAt($g(accompanyDisplayItems),"accompanyDisplayItems") 
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetEscortDataV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// 陪护体征
/// w ##class(Nur.MNISV3.Service.Accompany).GetEscortVitalSignDataV4("1217","2025-06-23","2025-06-23","8","20665","3","2","{""single"":""1""}")
ClassMethod GetEscortVitalSignDataV4(episodeId, fromDate, toDate, locId, userId, wardId, hospitalId, extras)
{
    n (episodeId, fromDate, toDate, locId, userId,wardId,hospitalId,extras,%session)
    s $zt="errGetEscortVitalSignDataV4"
    s ^GRS("GetEscortVitalSignDataV4")=$lb(episodeId, fromDate, toDate, locId, userId,wardId,hospitalId,extras)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s fromDate=##class(websys.Conversions).DateHtmlToLogical(fromDate)
    s toDate=##class(websys.Conversions).DateHtmlToLogical(toDate)
    s patInfoList=##class(%ListOfDataTypes).%New()
    //基本患者信息
    s patinfo=##class(%ArrayOfDataTypes).%New()
    s accompanyVitalSignData=##class(%ListOfDataTypes).%New()
    s escortId=..GetEscortIdAsStrAndMappingAdmIdAsObj(wardId,userId,hospitalId,episodeId)
    w escortId,!
    s accomRecordObj=##class(%ResultSet).%New("Nur.NIS.Service.Accom.NCPAccompany:GetNCPAccompanySub")
    d accomRecordObj.Execute($g(escortId),hospitalId,fromDate,toDate)
    while(accomRecordObj.Next()){
         s accomData=##class(Nur.MNISV3.Common.Utils).ResultSet(accomRecordObj)
         d accompanyVitalSignData.Insert(accomData)
    }
    d patinfo.SetAt($g(accompanyVitalSignData),"accompanyVitalSignData")
    s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
    d patInfoList.Insert(patinfo)
    s accompanyVitalSignDisplayItems= ..GetDisplayElementsByType(hospitalId,"T",
        "NCPARecInfo1,NCPARecInfo2,NCPARecInfo3,NCPARecInfo4,NCPARecInfo5,NCPARecInfo6,NCPARecInfo7,NCPARecInfo8,NCPARecInfo9,NCPARecInfo10","NCPASRecordDate,NCPASRecordTime" )
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    d json.SetAt($g(accompanyVitalSignDisplayItems),"accompanyVitalSignDisplayItems") 
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetEscortVitalSignDataV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// 陪护模板
/// w ##class(Nur.MNISV3.Service.Accompany).GetEscortTemplateV4("1217","","register","2")
ClassMethod GetEscortTemplateV4(episodeId, id, motion, hospitalId)
{
    n (episodeId, id, motion,hospitalId,%session)
    s $zt="errGetEscortTemplateV4"
    s ^GRS("GetEscortTemplateV4")=$lb(episodeId, id, motion,hospitalId)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s template=..GetTemplateByTypeAndSetValue(hospitalId,"T",episodeId,id)
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(template),"template")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetEscortTemplateV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// 保存、编辑
/// w ##class(Nur.MNISV3.Service.Accompany).EditEscortV4(
ClassMethod EditEscortV4(episodeId, id, motion, hospitalId, userId, form)
{
    n (episodeId, id, motion,hospitalId,userId,form,%session)
    s $zt="errEditEscortV4"
    s ^GRS("EditEscortV4")=$lb(episodeId, id, motion,hospitalId,userId,form)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s parseForm=##class(Nur.MNISV3.Common.JSON).Decode(form)
    ;;;陪护人员信息登记
    i (motion="register") {
        s NCPARIdNo=parseForm.GetAt("NCPARIdNo")
        s checkOrder=##class(Nur.NIS.Service.Accom.NCPAccompany).GetNCPAccompanyControl(episodeId,hospitalId)
        i (checkOrder="Y")&&($g(error)="") s error="患者未开陪护医嘱"
        s checkRepetition=##class(Nur.NIS.Service.Accom.NCPAccompany).GetRepeatCredNoRec(episodeId,id,NCPARIdNo)        
        i (checkRepetition="Y")&&($g(error)="") s error="身份证号："_NCPARIdNo_"已是该患者陪护人员"
        i ($g(error)="") {
            s output=##class(Nur.NIS.Service.Accom.NCPAccompany).SaveNCPAccompanyRec(id,form,userId)
            i '(output>0) s Msg=output,Status="-1"
        }
    }
    ;;;陪护人员体温记录
    if (motion="record") {
        s NCPARRowID=parseForm.GetAt("NCPARRowID")
        s NCPARSRowID=parseForm.GetAt("NCPARSRowID")
        i (id="") s id=NCPARSRowID
        s par=$p(id,"||",1)
        s sub=$p(id,"||",2)
        i (par="") s par=NCPARRowID
        s output=##class(Nur.NIS.Service.Accom.NCPAccompany).SaveNCPAccompanySub(par,sub,form,userId)
        i output'=0 s Msg=output,Status="-1"
    }
    //删除陪护记录
    if (motion="cancel") {   
        s output=##class(Nur.NIS.Service.Accom.NCPAccompany).DelCNCPAccompanyRec(id,userId)
        i output'=0 s Msg=output,Status="-1"
    }
    //删除体征数据记录
    if (motion="erase") {   
        s output=##class(Nur.NIS.Service.Accom.NCPAccompany).DelCNCPAccompanySub(id,userId)
        i output'=0 s Msg=output,Status="-1"
    }
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errEditEscortV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

ClassMethod GetTemplateByTypeAndSetValue(hospId, opType, episodeId, id) As %String
{
    n (hospId,opType,episodeId,id,%session)
    s template=..GetTemplateByType(hospId,opType)
    s panels=template.GetAt("template")
    s escortObj=..GetEscortObj(id)
    s panelLen=panels.Count()
    f panelInd=1:1:panelLen {
        s panel=panels.GetAt(panelInd)
        s elements=panel.GetAt("items")
        s elementLen=elements.Count()
        f elementInd=1:1:elementLen {
            s element=elements.GetAt(elementInd)
            s code=element.GetAt("AccomColCode")
            continue:(code="")
            i (##class(Nur.Tools).IsDefined(escortObj,code)) {
                s value=escortObj.GetAt(code)
            } else {
                s elementId=element.GetAt("ID")
                s value=..GetElementDefault(elementId,episodeId)
            }
            d element.SetAt(value,"AccomColDefaultVal")
        }
    }
    q panels
}

/// w ##class(Nur.MNISV3.Service.Accompany).GetEscortObj(1)
ClassMethod GetEscortObj(id) As %String
{
    n (id)
    s obj=##class(Nur.ACCOM.NCPAccompanyRecord).%OpenId(id)
    q:('$isobject(obj)) ""
    s keys="NCPARIdNo"
    f index=1:1:30 {
        s name="NCPAInfo"_index
        s keys=keys_","_name
    }
    s escortObj=##class(Nur.Tools).RapidProp(keys,obj)
    i ($isobject(obj.NCPARPaPmiDR)) {
        s NCPARegNo=obj.NCPARPaPmiDR.PAPMIIPNo
        s NCPAName=obj.NCPARPaPmiDR.PAPMIName
    }
    d escortObj.SetAt($g(NCPARegNo),"NCPARegNo")
    i ($g(NCPAName)'="") d escortObj.SetAt(NCPAName,"NCPAInfo1")
    d obj.%Close()
    q escortObj
}

ClassMethod GetElementDefault(elementId, episodeId) As %String
{
    n (elementId,episodeId,%session)
    s obj=##class(CF.NUR.Accom.Config).%OpenId(elementId)
    i ($isobject(obj)) {
        i (obj.AccomColUsing'="N") {
            s type=obj.AccomColType
            s code=obj.AccomColCode
            s expr=obj.AccomColFunc
            s default=obj.AccomColDefaultVal
            i (default'="") s expr=default
            i (expr'="") {
                s value=..Expr(expr,episodeId)
            } elseif (type="PAT") {
                s value= ##class(Nur.NIS.Service.Base.BedConfig).GetBasicDataByCode(0,code,episodeId)
            }
        }
        d obj.%Close()
    }
    q $g(value)
}

ClassMethod Expr(expr, episodeId) As %String
{
    n (expr,episodeId,%session)
    i (expr["episodeID") s episodeID=episodeId
    i (expr["papmiID") s papmiID=+$g(^PAADM(episodeId))
    x "s value="_expr
    q $g(value)
}

ClassMethod GetEscortIdAsStrAndMappingAdmIdAsObj(wardId, userId, hospitalId, episodeId) As %String
{
    n (wardId,userId,hospitalId,episodeId,%session)
    s map=##class(%ArrayOfDataTypes).%New()
    s escortId=""
     //1.调用PC查询
    s accomRecordObj=##class(%ResultSet).%New("Nur.NIS.Service.Accom.NCPAccompany:GetNCPAccompanyRecord")
    d accomRecordObj.Execute("","",wardId,"",userId,hospitalId,episodeId)
    while(accomRecordObj.Next()){
        s NCPARRowID=accomRecordObj.GetDataByName("NCPARRowID")
        i NCPARRowID'="" {
            i escortId="" s escortId=NCPARRowID
            e  s escortId=escortId_"^"_NCPARRowID
        }
    }
    q escortId
}

ClassMethod GetDisplayElementsByType(hospId, opType, bs, ss) As %String
{
    n (hospId,opType,bs,ss,%session)
    s items=##class(%ListOfDataTypes).%New()
    s lb=$lfs(bs)
    s ls=$lfs(ss)
    s template=..GetTemplateByType(hospId,opType)
    s panels=template.GetAt("template")
    s panelLen=panels.Count()
    f panelInd=1:1:panelLen {
        s panel=panels.GetAt(panelInd)
        s elements=panel.GetAt("items")
        s elementLen=elements.Count()
        f elementInd=1:1:elementLen {
            s element=elements.GetAt(elementInd)
            s code=element.GetAt("AccomColCode")
            i ($lf(lb,code)) {
                d items.Insert(element)
            } elseif ($lf(ls,code)=1) {
                s date=element
                d items.Insert(date)
            } elseif ($lf(ls,code)=2) {
                s time=element
                i ($d(date)) {
                    s children=##class(%ListOfDataTypes).%New()
                    d children.Insert(time)
                    d date.SetAt(children,"children")
                }
            }
        }
    }
    q items
}

ClassMethod GetTemplateByType(hospId, opType) As %String
{
    n (hospId,opType,%session)
    s panels=##class(%ListOfDataTypes).%New()
    s hospId=##class(web.DHCBL.BDP.BDPMappingHOSP).GetDefHospIdByTableName("NUR_Accom_Config",hospId,"")
    s colTypeArr=##class(Nur.HISUI.AccompanyConfig).getDisplayList("CF.NUR.Accom.Config","AccomColType")
    s colType="" f  s colType=$o(^CF.NUR.Accom.ConfigI(0,"AccomOPColType",hospId,opType,colType)) q:(colType="")  d
    .s title=colTypeArr.GetAt(colType)
    .s items=..GetElementsByType(hospId,opType,colType)
    .q:(items.Count()=0)
    .s panel=##class(Nur.Tools).RapidAssembly("title,items",title,items)
    .d panels.Insert(panel)
    q ##class(Nur.Tools).RapidAssembly("template",panels)
}

ClassMethod GetElementsByType(hospId, opType, colType) As %String
{
    n (hospId,opType,colType)
    s items=##class(%ListOfDataTypes).%New()
    s sortNo="" f  s sortNo=$o(^CF.NUR.Accom.ConfigI(0,"AccomOPColType",hospId,opType,colType,sortNo)) q:(sortNo="")  d
    .s id="" f  s id=$o(^CF.NUR.Accom.ConfigI(0,"AccomOPColType",hospId,opType,colType,sortNo,id)) q:(id="")  d
    ..s item=..GetElementObj(id)
    ..q:(item="")
    ..d items.Insert(item)
    q items
}

ClassMethod GetElementObj(id) As %String
{
    n (id)
    s obj=##class(CF.NUR.Accom.Config).%OpenId(id)
    i ($isobject(obj)) {
        i (obj.AccomColUsing'="N") {
            s itemObj=##class(Nur.Tools).RapidProp("AccomOPType,AccomColType,AccomColCode,AccomColDesc,AccomColDataType,AccomColValue,AccomColWarning",obj)
            d itemObj.SetAt(id,"ID")
        }
        d obj.%Close()
    }
    q $g(itemObj)
}

}
